<?php
require 'common.inc.php';
require DT_ROOT.'/api/weixin/init.inc.php';
$url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$access_token.'&next_openid='.$next_openid;
echo "url =>".$url.'<br>';
//遍历获取所有用户信息
$result = $db->query("SELECT * FROM {$DT_PRE}weixin_user where pushstatus=3 or pushstatus=4  ORDER BY itemid ");
$curr_time = time();
sleep(rand(0,10));
if($db->affected_rows($result)) {
    while($user = $db->fetch_array($result)) {
		$pushlist = pushlist_weixin($user,$curr_time);         //获取推送的查询列表
		$_openid = $user['openid'];
		$nickname = $user['nickname'];
		$cur_time = time();
		$u_pushtime = $db->get_one("SELECT pushtime FROM {$DT_PRE}weixin_user WHERE openid ='$_openid'");

		echo  '[nickname]'. $user['nickname'].'[openid]'.$_openid.'=><br>';

		$r  =  $db->get_one("SELECT * FROM {$DT_PRE}pushlog where openid = '{$_openid}'  ORDER BY createtime desc limit 1 ");
		$db->query("INSERT INTO {$db->pre}pushlog (openid,nickname,createtime) VALUES ('{$_openid}','{$nickname}','{$cur_time}')");
		if($r){
			if(time() - $r['createtime'] < 60 ){
				continue;
			}
		}

		if(!$pushlist || empty($user['filtercategory']) ){
			echo  '=> nodata to push ......<br>';
		    continue;
		} 
		//10分钟内不再重新发送，解决同时间发送多条重复信息
		if( time() - $u_pushtime['pushtime'] < 600 ){
			echo 'current time no need push msg......<br>';
		    continue;
		} 

    	if($user['pushwx'] || $user['pushemail']){
            $md5 = md5(uniqid());
    		$DT['push_filename']        = 'html_push/push-'.$md5;
            $DT['push_wx_filename']     = 'html_push/push-'.$md5.'-wx';
            //$DT['push_email_filename']  = 'html_push/push-'.$md5.'-email';
        	//生成html
        	tohtml('push');
        	$filename = $CFG['com_dir'] ? DT_ROOT.'/'.$DT['push_filename'].'.'.$DT['file_ext'] : DT_CACHE.'/index.inc.html';
        	echo  'filename=>'.$filename.'</br>' ;
        }
        
        $_pushtime = time();
      //  $db->query("update {$DT_PRE}weixin_user  set pushtime = '$_pushtime' where openid ='$_openid'");

        /*****************************
         *  发送微信
         * *************************/

        // if($user['pushwx']){
           // echo 'pushwx->user['.$user['nickname'].']===></br>';
	//		$title=$pushlist[0]['title'];
	//		$redis = new Redis(); 
	//	    $redis->connect('10.10.2.117', 6379); 
	//		$bb=md5($title.$user['nickname']);
	//		$a=$redis->hSet('b', $bb, $bb); 
	//		if($a==1){
//				send_wxs($DT['push_wx_filename'],count($pushlist));
	//		}
           
        //}

    }
}


function send_wxs($filename,$zs) {
	global $wx;
    global $access_token;
    global $DT;
    global $MODULE;
    global $user;
    $CAT_TENDER = cache_read('category-23.php');
    $data = array();
    $data['first'] = array('value'=>"您有新的招标信息！",'color'=>'#173177');
    $data['Good'] = array('value'=>"合信推送",'color'=>'#173177');
    $data['contentType'] = array('value'=>"招标信息",'color'=>'#173177');
    $str_remark = '';
   $pushtest = pushlist_weixin($user);
   $count = 0;
    if (is_array($pushtest)) {
        foreach ($pushtest as $t) {
           $count++;
            //如果标题超过500字则进行截取，补上字符 ...(共X条)
            //经过调试后 350  字截取 440 的不会出现乱码，发送不会丢失【刚刚发布的】信息；
            if(strlen($str_remark) > 300){
                $str_remark = dsubstr($str_remark,250).'
...(共'.$zs.'条)';
				echo "<br>remark===>".$str_remark;
                break;
            }
            $filtertender = explode(",",$t['catid'] );
            if(!empty($filtertender)){
                $arr = array();
                foreach ($filtertender as $v){
                    $cat = $CAT_TENDER[$v];
                    if(!empty($cat)){
                        $arr[] = $cat['catname'];
                    }
                }
                $t['catid'] =implode(',',$arr);
            }

                $str_remark.='
'.$count.' '.  $t['title'].'！['.$t['filtercategory'].']['.$t['filteraddress'].']['.$t['catid'].']';
     }

}
    $to_url = 'http://www.china-hxzb.com/'. $filename.'.'.$DT['file_ext'];
    $str_remark = preg_replace('/\(\d*\)/','',$str_remark);
    $data['remark'] = array('value'=>'刚刚发布的：'.$str_remark,'color'=>'#173177');
    $openid = $user['openid'];
    $arr = $wx->send_template($openid, '1BrT6-eNP_hVpGr1XxE5Vtz6sAReINF-7s-VyMv05YY', $data,$access_token,$to_url);
    if($arr['errcode'] == '40001'){
        global $dc ;
        $access_token = $wx->get_token();
        $dc->set('weixin_access_token', $access_token, 7000);
    } 
}
?>
